package com.shanzmoo.base.bean;

import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.base.constant.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 基础响应类
 * @author: Gzhao 2020/11/6
 */
@Data
public class SResponse<T> implements Serializable {

    private int errno;
    /**
     * 返回消息
     */
    private String errmsg;
    private T data;

    private SResponse() {}

    private SResponse(Boolean success, Integer errno, T data, String errmsg) {
        this.errno = success ? Constant.SUCCESS_CODE :errno;
        this.errmsg = success ? "成功" : errmsg;
        this.data = data;
    }

    private SResponse(Boolean success, Integer errno, String errmsg) {
        this.errno = success ? Constant.SUCCESS_CODE :errno;
        this.errmsg = errmsg;
//        this.data = ;
    }


    public static <T> SResponse<T> ok() {
        return new SResponse<T>(true, null, null);
    }

    public static <T> SResponse<T> ok(T data) {
        return new SResponse<T>(true, null, data, null);
    }

    public static <T> SResponse<T> fail(int errno, String errmsg) {
        return new SResponse<T>(false, errno, errmsg);
    }

    public static <T> SResponse<T> fail(ErrorCode errorCode) {
        return new SResponse<T>(false, errorCode.code(), errorCode.desc());
    }

    public boolean isOk() {
        return errno == Constant.SUCCESS_CODE;
    }

}


